<template>
  <div class="my-team-detail">
    <div class="header">
      <span class="back" @click="goBack">&lt;</span>
      <span class="title">班组详情</span>
      <span class="star"></span>
    </div>
    <div class="main">
      <div class="team-title">{{ team.name }}</div>
      <div class="team-meta">
        <span class="count">共 {{ team.members }} 人</span>
        <span class="tag">组员</span>
      </div>
      <div class="btn-row">
        <button class="stat-btn" @click="showTeamStats">任务完成统计</button>
        <button class="quit-btn" @click="onQuit">退出班组</button>
      </div>
      <div class="intro">
        {{ team.intro }}
      </div>
      <div class="members">
        <div class="member-card" v-for="m in team.memberList || []" :key="m.id">
          <img :src="m.avatar" class="avatar" />
          <div class="info">
            <div class="row">
              <span class="name">{{ m.name }}</span>
              <span v-if="m.leader" class="leader-tag">组长</span>
            </div>
            <div class="phone">{{ m.phone }}</div>
          </div>
        </div>
      </div>
    </div>
    <div v-if="showConfirm" class="confirm-mask">
      <div class="confirm-dialog">
        <div class="confirm-msg">确定要退出该班组吗？</div>
        <div class="confirm-btns">
          <button class="confirm-cancel" @click="cancelQuit">取消</button>
          <button class="confirm-ok" @click="confirmQuit">确认退出</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed, ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useTeamStore } from '../stores/TeamStatsStore.js'

const router = useRouter()
const route = useRoute()
const teamStore = useTeamStore()

const team = computed(() => teamStore.teams.find(t => t.id === parseInt(route.params.id)) || {})
const showConfirm = ref(false)

function onQuit() {
  showConfirm.value = true
}

function confirmQuit() {
  teamStore.unjoinTeam(parseInt(route.params.id))
  showConfirm.value = false
  // 返回上一页
  goBack()
}

function cancelQuit() {
  showConfirm.value = false
}

function goBack() {
  router.back()
}

function showTeamStats() {
  router.push(`/team-stats/${route.params.id}`)
}
</script>

<style scoped>
.my-team-detail {
  background: #f7f8fa;
  min-height: 100vh;
  max-width: 375px;
  margin: 0 auto;
  box-sizing: border-box;
}
.header {
  display: flex;
  align-items: center;
  height: 48px;
  background: #fff;
  font-size: 18px;
  font-weight: bold;
  border-bottom: 1px solid #f0f0f0;
  padding: 0 16px;
  justify-content: space-between;
}
.back {
  color: #333;
  font-size: 22px;
  cursor: pointer;
}
.title {
  flex: 1;
  text-align: center;
  font-size: 17px;
  font-weight: 500;
}
.star {
  width: 22px;
  height: 22px;
  display: inline-block;
  background: url('data:image/svg+xml;utf8,<svg width="22" height="22" viewBox="0 0 22 22" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M11 2.5l2.472 6.13 6.528.47-5.1 4.47 1.6 6.43L11 16.1l-5.5 3.9 1.6-6.43-5.1-4.47 6.528-.47L11 2.5z" stroke="%23FFD700" stroke-width="1.5" fill="none"/></svg>') center/contain no-repeat;
}
.main {
  padding: 0 16px;
}
.team-title {
  font-size: 19px;
  font-weight: bold;
  color: #222;
  margin: 18px 0 0 0;
}
.team-meta {
  color: #888;
  font-size: 14px;
  margin: 6px 0 0 0;
  display: flex;
  align-items: center;
  gap: 8px;
}
.count {}
.tag {
  background: #e6f0ff;
  color: #409eff;
  border-radius: 4px;
  padding: 1px 8px;
  font-size: 13px;
}
.btn-row {
  display: flex;
  gap: 12px;
  margin: 18px 0 0 0;
}
.stat-btn {
  flex: 1;
  background: #409eff;
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 12px 0;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
}
.quit-btn {
  flex: 1;
  background: #f45ca4;
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 12px 0;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
}
.intro {
  color: #444;
  font-size: 14px;
  margin: 18px 0 0 0;
  line-height: 1.7;
}
.members {
  display: flex;
  flex-wrap: wrap;
  gap: 12px 10px;
  margin: 18px 0 0 0;
}
.member-card {
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 1px 4px #f0f1f2;
  width: calc(50% - 5px);
  display: flex;
  align-items: center;
  padding: 10px 8px;
  margin-bottom: 0;
}
.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  object-fit: cover;
  margin-right: 10px;
}
.info {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.row {
  display: flex;
  align-items: center;
  gap: 6px;
}
.name {
  font-weight: bold;
  font-size: 15px;
  color: #222;
}
.leader-tag {
  background: #ffe066;
  color: #bfa100;
  border-radius: 4px;
  font-size: 12px;
  padding: 1px 6px;
  margin-left: 2px;
}
.phone {
  color: #888;
  font-size: 13px;
}
.confirm-mask {
  position: fixed;
  left: 0; top: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.18);
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
}
.confirm-dialog {
  background: #fff;
  border-radius: 10px;
  padding: 28px 32px 18px 32px;
  box-shadow: 0 2px 16px #e6eaf3;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.confirm-msg {
  font-size: 18px;
  color: #222;
  margin-bottom: 18px;
}
.confirm-btns {
  display: flex;
  gap: 18px;
}
.confirm-cancel {
  background: #eee;
  color: #666;
  border: none;
  border-radius: 6px;
  padding: 8px 24px;
  font-size: 15px;
  cursor: pointer;
}
.confirm-ok {
  background: #f45ca4;
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 8px 24px;
  font-size: 15px;
  cursor: pointer;
}
</style> 